//
//  MyLeftRightMovingButton.cpp
//  CanYouDoIt
//
//  Created by hoangtx on 7/19/13.
//
//

#include "MyLeftRightMovingButton.h"


bool MyLeftRightMovingButton::init(){
    MySpriteButton::init();
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    velocity = winSize.width/90;
    int randomDirection = rand()%2;
    if(randomDirection==0){
        velocity = -velocity;
    }

    return true;
}

void MyLeftRightMovingButton::handleUpdate(float dt){
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    setPosition(ccp(getPositionX()+velocity,getPositionY()));
    if(getPositionX()<0){
        setPosition(ccp(-getPositionX(),getPositionY()));
        velocity = - velocity;
    }else{
        if(getPositionX()+getBoundingRect().size.width>winSize.width){
            setPosition(ccp(2*winSize.width -(getPositionX()+2*getBoundingRect().size.width),getPositionY()));
            velocity = - velocity;
        }
    }
}

void MyLeftRightMovingButton::changeDirection(){
    velocity = -velocity;
}

void MyLeftRightMovingButton::goBackToOriginal(){
    int randomDirection = rand()%2;
    if(randomDirection==0){
        velocity = -velocity;
    }
    setPosition(originalPosition);

}